package org.nvframe.component.property;

import java.util.ArrayList;
import java.util.List;

import org.nvframe.component.property.operation.*;

public class TransientPropertyInt implements TransientProperty {
	
	private int persistentVal;
	private int cachedTransientVal;
	
	private List<Operation> operations;

	public TransientPropertyInt(int persistentVal) {
		this.persistentVal = persistentVal;
		
		operations = new ArrayList<Operation> ();
		cachedTransientVal = 0;
	}

	private int reCalculateCachedValue() {
		int val = persistentVal;
		
		for(Operation operation : operations) {
			val = operation.getResult(val);
		}
		
		cachedTransientVal = val;
		
		return val;
	}
	
	public int getPersistentVal() {
		return persistentVal;
	}
	
	public int getValue() {
		return cachedTransientVal;
	}
	
	public void add(int addVal) {
		operations.add(new Addition(addVal));	
		reCalculateCachedValue();
	}
	
	public void substract(int substractVal) {
		operations.add(new Substraction(substractVal));	
		reCalculateCachedValue();
	}
	
	public void multiply(int multiplyVal) {
		operations.add(new Multiplication(multiplyVal));	
		reCalculateCachedValue();
	}
	
	public void divide(int divideVal) {
		operations.add(new Division(divideVal));	
		reCalculateCachedValue();
	}
	
	public void percentage(int percentage) {
		operations.add(new Percentage(percentage));	
		reCalculateCachedValue();
	}
	
	
}
